const app = getApp();
var api = require('../../../api.js');
var interval = null

Page({
  data: {
    disabled: false,
    truename: '',
    mobile: '',
    vcode: '',
    time: "获取验证码",
    currentTime: 60,
  },
  onLoad: function (options) {

    app.bind();
    this.getinfo();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {

    app.bind();
    this.getinfo();
    
    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };
  },
  getinfo: function (e) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.user.info,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        if (e.data.code == 0) {
          that.setData({
            truename: e.data.data.truename,
            mobile: e.data.data.mobile
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  name: function (e) {
    // console.log(e);
    var name = e.detail.value;
    this.setData({
      truename: name
    })
  },
  mobile: function (e) {
    var mobile = e.detail.value;
    this.setData({
      mobile: mobile
    })
  },
  vcode: function (e) {
    var vcode = e.detail.value;
    this.setData({
      vcode: vcode
    })
  },
  // 获取验证吗
  getVcode: function() {
    var that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    // console.log(that.data.phone.length)
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token);
    console.log(that.data.mobile);
    
    if (access_token == ''){
      wx.redirectTo({
        url: '/pages/grant/grant',
      })
    }else{
      that.setData({
        judge: true
      });
      that.setData({
        time: '60秒'
      });
      that.getCode();
      that.setData({
        disabled: true
      });

      wx.request({
        url: api.user.sms,
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          
          access_token: access_token,
          // truename: that.data.truename,
          mobile: that.data.mobile,
          // yzm: that.data.mobile,
          type: 2,
        },
        success: function (e) {
          console.log(e)
        }
      }); 

    }
  },
  getCode: function (options){
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime+'秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval);
        that.setData({
          time: '重新发送',
          currentTime:60,
          disabled: false
        })
      }
    }, 1000)
  },
  modify: function (e) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var trueName = that.data.truename;
    var mobile = that.data.mobile;
    var vcode = that.data.vcode;
    wx.request({
      url:api.user.yzm,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        truename: trueName,
        mobile: mobile,
        yzm: vcode,
      },
      success: function (e) {
        console.log(e);
        if (e.data.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        }
      },
      complete: function (e) {
        var title = e.data.data;
        wx.showToast({
          title: title,
          icon: 'none',
        });        
      }
    });
  },
})